package com.example.gmr.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import com.example.gmr.Helper.SharedPreferenceHelper;
import com.example.gmr.R;

public class Register extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{

    //创建控件对象
    private CheckBox checkBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    private CheckBox checkBox4;
    private Switch swh_status;
    private Button commit,change;
    private EditText re_username,re_password,re_password_2;
    private String str_username,str_password,str_password_2;
    private SharedPreferenceHelper helper;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        change = (Button) findViewById(R.id.change);
        final ImageView touxiang = (ImageView) findViewById(R.id.touxiang);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                touxiang.setImageResource(R.drawable.logo);
            }
        });
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                Toast.makeText(Register.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkBox4);

        //获取监听
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        checkBox4.setOnCheckedChangeListener(this);

        swh_status = (Switch) findViewById(R.id.switch1);
        swh_status.setOnCheckedChangeListener(this);

        context=getApplicationContext();
        helper=new SharedPreferenceHelper(context);

        commit=(Button)findViewById(R.id.commit);
        re_username=(EditText)findViewById(R.id.re_username);
        re_password=(EditText)findViewById(R.id.re_password);
        re_password_2=(EditText)findViewById(R.id.re_password_2);
        commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder=new AlertDialog.Builder(Register.this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("提交");
        builder.setMessage("您是否将信息提交");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                str_username = re_username.getText().toString();
                str_password = re_password.getText().toString();
                str_password_2=re_password_2.getText().toString();
                if (str_password_2.equals(str_password)){
                    helper.save(str_username, str_password);
                    Intent intent = new Intent(context, Main.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(context,"两次密码输入不相同，请重新输入",Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.create();
        builder.show();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch1:
                if (buttonView.isChecked())
                    Toast.makeText(this,"安全",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(this,"不安全",Toast.LENGTH_SHORT).show();
                break;

        }
    }
}
